<?php



use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Excel;

class ContactController {

    public function listTasks(Request $request){


        //check login
        $sid = $request->header('sid');
        if(empty($sid)){
            $result_arr = array(
                'code'=>'1',
                'msg'=>'未登录，sid为空'
            );
            return response()->json($result_arr);
        }else{
            $users = DB::table('users')->where("sid", "=", $sid)->get();
            if(count($users) == 0){
                $result_arr = array(
                    'code'=>'2',
                    'msg'=>'用户不存在'
                );
                return response()->json($result_arr);
            }
        }

		$user_id = $request->input('user_id');
		// $bywho = $request->has('bywho') ? $request->input('bywho') : 'user';
		// $pageNow = $request->input('pageNow');
		// $pageSize = $request->input('pageSize');

		//full, location, none
		$type = $request->input('type');
		$keyword = $request->input('keyword'); //全文，或者lnt,lat

		if(empty($sid)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'未登录',
					'result' => "[]"
			);
			return response()->json($result_arr);
		}
		if(empty($user_id)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'请传入user_id',
					'result' => "[]"
			);
			return response()->json($result_arr);
		}


		$tasks = array();
		$tasks = DB::table('contact')->where("user_id", "=", $user_id)->orderBy('id','desc')->get();
		
		if($type === "full"){
			if(empty($keyword)){
				$result_arr = array(
						'code'=>'1',
						'msg'=>'请输入关键词',
						'result' => "[]"
				);
				return response()->json($result_arr);
			}
			/// 全文拼接吧

		}else if($type === "location"){
			if(empty($keyword)){
				$result_arr = array(
						'code'=>'1',
						'msg'=>'请输入lnt,lat，注意逗号拼起来',
						'result' => "[]"
				);
				return response()->json($result_arr);
			}
			///计算经纬度距离吧
		}

		$result_arr = array(
				'code'=>'0',
				'msg'=>'ok',
				'result' => $tasks
				);

    	//return response(json_encode($result_arr));
		return response()->json($result_arr);
		
    }

	public function addTask(Request $request){

        //check login
        $sid = $request->header('sid');
        if(empty($sid)){
            $result_arr = array(
                'code'=>'1',
                'msg'=>'未登录，sid为空'
            );
            return response()->json($result_arr);
        }else{
            $users = DB::table('users')->where("sid", "=", $sid)->get();
            if(count($users) == 0){
                $result_arr = array(
                    'code'=>'2',
                    'msg'=>'用户不存在'
                );
                return response()->json($result_arr);
            }
        }



        $contact_id = $request->input('contact_id');
		$user_id = $request->input('user_id');
		$real_name = $request->input('real_name');
		$phone = $request->input('phone');
		$birthday = $request->input('birthday');
		$gender = $request->input('gender');
		$shengxiao = $request->input('shengxiao');
		$marriage = $request->input('marriage');
		$company = $request->input('company');
		$department = $request->input('department');
		$title = $request->input('title');
		$lnt = $request->input('lnt');
		$lat = $request->input('lat');
		$addr = $request->input('addr');
		$extra = $request->input('extra');

		$result_arr = array();


		if(empty($user_id)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入user_id'
			);
			return response()->json($result_arr);
		}
		if(empty($real_name)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入客户姓名'
			);
			return response()->json($result_arr);
		}
		if(empty($phone)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入手机号'
			);
			return response()->json($result_arr);
		}
		if(empty($birthday)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入生日'
			);
			return response()->json($result_arr);
		}
		if(empty($gender)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入性别'
			);
			return response()->json($result_arr);
		}
		if(empty($shengxiao)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入生肖'
			);
			return response()->json($result_arr);
		}
		if(empty($marriage)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入婚姻状况'
			);
			return response()->json($result_arr);
		}
		if(empty($company)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入公司'
			);
			return response()->json($result_arr);
		}
		if(empty($department)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入部门'
			);
			return response()->json($result_arr);
		}
		if(empty($title)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入职位'
			);
			return response()->json($result_arr);
		}
		if(empty($lnt)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入lnt'
			);
			return response()->json($result_arr);
		}
		if(empty($lat)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入纬度'
			);
			return response()->json($result_arr);
		}
		if(empty($addr)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须传入详细地址'
			);
			return response()->json($result_arr);
		}

		if(empty($contact_id) || $contact_id == -1){
			//新建
			$contact_id = DB::table('contact')->insertGetId([
					'user_id'=>$user_id,
					'real_name'=>$real_name,
					'phone'=>$phone,
					'birthday'=>$birthday,
					'gender'=>$gender,
					'shengxiao'=>$shengxiao,
					'marriage'=>$marriage,
					'company'=>$company,
					'department'=>$department,
					'title'=>$title,
					'lnt'=>$lnt,
					'lat'=>$lat,
					'addr'=>$addr,
					'extra'=>$extra,
					'create_time'=>time() . "",
					'update_time'=>time() . ""
				]);
			$result_arr = array(
					'code'=>'0',
					'msg'=>'ok',
					'result'=>array(
							'contact_id'=>$contact_id
					)
			);

		}else{
			//update--结束
			DB::table('contact')->where('id','=', $contact_id)->update([
				'real_name'=>$real_name,
				'phone'=>$phone,
				'birthday'=>$birthday,
				'gender'=>$gender,
				'shengxiao'=>$shengxiao,
				'marriage'=>$marriage,
				'company'=>$company,
				'department'=>$department,
				'title'=>$title,
				'lnt'=>$lnt,
				'lat'=>$lat,
				'addr'=>$addr,
				'extra'=>$extra,
				'update_time'=>time() . ""
			]);
			$result_arr = array(
					'code'=>'0',
					'msg'=>'ok',
					'result'=>array(
							'contact_id'=>$contact_id
					)
			);
		}

		return response()->json($result_arr);
	}

	public function deleteTask(Request $request){

        //check login
        $sid = $request->header('sid');
        if(empty($sid)){
            $result_arr = array(
                'code'=>'1',
                'msg'=>'未登录，sid为空'
            );
            return response()->json($result_arr);
        }else{
            $users = DB::table('users')->where("sid", "=", $sid)->get();
            if(count($users) == 0){
                $result_arr = array(
                    'code'=>'2',
                    'msg'=>'用户不存在'
                );
                return response()->json($result_arr);
            }
        }


        $contact_id = $request->input('contact_id');

		$result_arr = array();

		if(empty($sid)){
			$result_arr = array(
					'code'=>'1',
					'msg'=>'未登录，sid为空'
			);
			return response()->json($result_arr);
		}

		if(empty($contact_id)){
			//新建
			$result_arr = array(
					'code'=>'1',
					'msg'=>'必须指定要删除的id'
			);
			return response()->json($result_arr);

		}else{
			//update--结束
			DB::table('contact')->where('id','=', $contact_id)->delete();
			$result_arr = array(
					'code'=>'0',
					'msg'=>'ok',
					'result'=>array()
			);
		}

		return response()->json($result_arr);
	}

}

?>